package com.zjmzxfzhl.modules.purchase.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.Result;
import com.zjmzxfzhl.common.base.BaseController;
import com.zjmzxfzhl.common.util.FileUtil;
import com.zjmzxfzhl.modules.purchase.entity.PurchaseSupplier;
import com.zjmzxfzhl.modules.purchase.service.PurchaseSupplierService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.util.List;

/**
 * Controller
 *
 * @author 庄金明
 */
@RestController
@RequestMapping("/purchase/supplier")
public class PurchaseSupplierController extends BaseController {
    @Autowired
    private PurchaseSupplierService purchaseSupplierService;

    /**
     * 自定义查询列表
     *
     * @param purchaseSupplier
     * @param current
     * @param size
     * @return
     */
    @RequiresPermissions("purchase:supplier:list")
    @GetMapping(value = "/list")
    public Result list(PurchaseSupplier purchaseSupplier, @RequestParam Integer current, @RequestParam Integer size) {
        IPage<PurchaseSupplier> pageList = purchaseSupplierService.list(new Page<PurchaseSupplier>(current, size), purchaseSupplier);
        return Result.ok(pageList);
    }

    /**
     * 查询所有供应商
     *
     * @return
     */
    @RequiresPermissions("purchase:supplier:list")
    @GetMapping(value = "/listAll")
    public Result listAll() {
        List<PurchaseSupplier> suppliers = purchaseSupplierService.list();
        return Result.ok(suppliers);
    }

    @RequiresPermissions("purchase:supplier:list")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        PurchaseSupplier purchaseSupplier = purchaseSupplierService.getById(id);
        return Result.ok(purchaseSupplier);
    }

    /**
     * @param purchaseSupplier
     * @return
     * @功能：新增
     */
    @RequiresPermissions("purchase:supplier:save")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody PurchaseSupplier purchaseSupplier) {
        purchaseSupplierService.save(purchaseSupplier);
        return Result.ok();
    }

    /**
     * @param purchaseSupplier
     * @return
     * @功能：修改
     */
    @RequiresPermissions("purchase:supplier:update")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody PurchaseSupplier purchaseSupplier) {
        purchaseSupplierService.updateById(purchaseSupplier);
        return Result.ok();
    }

    /**
     * @param ids
     * @return
     * @功能：批量删除
     */
    @RequiresPermissions("purchase:supplier:delete")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        String[] idsArr = ids.split(",");
        purchaseSupplierService.deleteSuppliers(Convert.toList(Long.class, idsArr));
        return Result.ok();
    }

    /**
     * 导出供应商-采购清单
     *
     * @param response
     * @param supplierId
     * @param startDate
     * @param endDate
     */
    @GetMapping(value = "/export")
    public void export(HttpServletResponse response, @RequestParam("supplierId") Long supplierId,
                       @RequestParam(value = "startDate", required = false) String startDate,
                       @RequestParam(value = "endDate", required = false) String endDate) {
        File exportFile = purchaseSupplierService.exportDetail(supplierId, startDate, endDate);
        FileUtil.setDownloadResponse(response, exportFile, exportFile.getName(), true);
    }


    @GetMapping(value = "/mergeSupplier")
    public Result mergeSupplier() {
        Boolean res = purchaseSupplierService.mergeSupplier();
        return Result.ok(res);
    }

    @GetMapping(value = "/replaceSupplier")
    public Result replaceSupplier(@RequestParam("oids") String oids, @RequestParam("nid") Long nid) {
        if (StringUtils.isEmpty(oids) || oids.trim().length() == 0) {
            return Result.error("oids can't be empty");
        }
        String[] oidsArr = oids.split(",");
        List<Long> oidList = Convert.toList(Long.class, oidsArr);
        oidList.remove(nid);
        purchaseSupplierService.replaceSuppliers(oidList, nid);
        return Result.ok();
    }
}
